package org.example.springmvc.controller;

import org.example.springmvc.pojo.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * ClassName: Hello04Controller
 * Package: org.example.springmvc.controller
 * Description:  获取请求参数
 *
 * @Author wanghao
 * @Create 2024-07-11 9:48
 * @Version 1.0
 */
@Controller
@RequestMapping("/hello04")
public class Hello04Controller {

    /**
     * 演示一个名字对应一个值
     * http://localhost:8080/hello04/h01?name=猪小明&age=18
     * @param name
     * @param age
     * @return
     */
    @RequestMapping("/h01")
    //public String h01(String name, Integer age) {
    public String h01(@RequestParam(value = "name", required = false, defaultValue = "猪小明") String name, @RequestParam(value = "age",defaultValue = "18") Integer age) {
        System.out.println(name + " " + age);
        return "succ";
    }


    /**
     * 演示一个名字对应多个值
     * http://localhost:8080/hello04/h02?hobby=篮球&hobby=足球
     * @param hobbys
     * @return
     */
    @RequestMapping("/h02")
    public String h02(@RequestParam("hobby") List<String> hobbys) {
        hobbys.forEach(System.out::println);
        return "succ";
    }


    /**
     * 演示表单对应实体模型
     * http://localhost:8080/hello04/h03?pid=p01&pname=猪小明&age=18...
     * @return
     */
    @RequestMapping("/h03")
    public String h03(Person person) {
        System.out.println(person);
        return "succ";
    }

    // 演示表单对应模型，但是模型中有级联的实体
    @RequestMapping("/h04")
    public String h04(Person person) {
        System.out.println(person);
        return "succ";
    }

    // 5. 演示表单对应模型
}
